package com.smarthealthring.mm.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.smarthealthring.mm.R;
import com.smarthealthring.mm.base.BaseActivity;
import com.smarthealthring.mm.contract.ResetPwdContract;
import com.smarthealthring.mm.presenter.ResetPwdPresenter;
import com.smarthealthring.mm.utils.ComToolBar;
import com.smarthealthring.mm.utils.RandomUtil;
import com.smarthealthring.mm.utils.ToastUtil;
import com.smarthealthring.mm.widgets.CleanEditText;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.HashMap;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * 重置密码
 * Created by bilibili on 2018/5/27.
 */

public class ResetPwdActivity extends BaseActivity<ResetPwdPresenter> implements ResetPwdContract.View{

    @BindView(R.id.tv_ensure)
    TextView mTvEnsure;
    @BindView(R.id.et_new_pwd)
    CleanEditText mEtNewPwd;
    @BindView(R.id.et_new_pwd_again)
    CleanEditText mEtNewPwdAgain;


    @Override
    protected void initComToolBar() {
        ComToolBar.setTitle(mContext,"重置密码");
        ImageView ivLeft = ComToolBar.setleftImg(mContext, R.drawable.back_white);
        RxView.clicks(ivLeft)
                .subscribe((Consumer) o -> finish());
    }

    @Override
    protected int getLayout() {
        return R.layout.act_reset_pwd;
    }

    @Override
    protected void initEventAndData(@Nullable Bundle savedInstanceState) {
        String captchamsg = getIntent().getStringExtra("captchamsg"); //验证码
        String username = getIntent().getStringExtra("username");//手机号

        //获取短信验证码
        RxView.clicks(mTvEnsure)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        String newPwd = mEtNewPwd.getText().toString().trim();
                        String newPwdAgain = mEtNewPwdAgain.getText().toString().trim();
                        if (TextUtils.isEmpty(newPwd)) {
                            ToastUtil.shortShow("请输入新密码");
                            return;
                        }
                        if (TextUtils.isEmpty(newPwdAgain)) {
                            ToastUtil.shortShow("请再次输入新密码!");
                            return;
                        }
                        if (!newPwd.equals(newPwdAgain)) {
                            ToastUtil.shortShow("两次密码不一致!");
                            return;
                        }

                        mPresenter.resetPwd(new HashMap<String, String>() {{
                            put("trans_id", "2005");
                            put("nonce_str", RandomUtil.getRandomString(32));
                            put("username", username);
                            put("captchamsg", captchamsg);
                            put("newpwd", newPwd);
                        }});
                    }
                });
    }

    @Override
    protected ResetPwdPresenter createPresenter() {
        return new ResetPwdPresenter();
    }

    @Override
    public void resetPwdSuccess() {
        startActivity(new Intent(mContext,LoginActivity.class));
    }

    @Override
    public void resetPwdFail() {

    }
}
